﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using RBS.Common;
using RBS.Core.Model.Hotel;

namespace RBS.Web.Hotel.Controllers
{
    public class BaseController : Controller
    {
        //
        // GET: /Base/
        public void ChkAdminLevel(string s, string toString)
        {
            //  throw new NotImplementedException();
        }
        public BaseController()
        {
           /* if (CurrentUser.HotelId == 0)
            {
                RedirectToAction("HotelEdit", "Hotel");
            }*/
        } 

        public bool IsHotelLogin()
        {
            //如果Session为Null
            if (Session[RBSKeys.SESSION_HOTEL_INFO] != null)
            {
                return true;
            }
            else
            {
                //检查Cookies
                string adminname = Utils.GetCookie("AdminName", "DTcms");
                string adminpwd = Utils.GetCookie("AdminPwd", "DTcms");
                if (adminname != "" && adminpwd != "")
                {
                    var bll = new RBS.Core.BLL.Hotel.H_Hotel_Users();

                    var model = bll.UserLogin(adminname, adminpwd);
                    if (model != null)
                    {
                        Session[RBSKeys.SESSION_HOTEL_INFO] = model;
                        return true;
                    }
                }
            }
            return false;
        }
        public RBS.Core.Model.Hotel.H_Hotel_Users CurrentUser
        {
            get
            {

                var hotelUser = (H_Hotel_Users)Session[RBSKeys.SESSION_HOTEL_INFO];
                if (hotelUser==null || hotelUser.HotelId == 0)
                {
                    if (IsHotelLogin()) 
                        hotelUser = (H_Hotel_Users)Session[RBSKeys.SESSION_HOTEL_INFO];
                    else
                    {
                        RefCurrentUser();
                    }
                }
                return hotelUser;
            }
            set { Session[RBSKeys.SESSION_HOTEL_INFO] = value; }
        } 
        public void RefCurrentUser()
        {  
          Response.Redirect("~/login/index");
        }

        public IList< H_Hotels_Role> HotelsRole
        {
            get
            {
                if (Session["HotelsRole"] == null)
                {
                    IList<H_Hotels_Role> hHotelsRoles = new RBS.Core.BLL.Hotel.H_Hotels_Role().GetModelList("");
                    //是否为加密数据
                    /* if (AppConfig.PasswordPolicy.ToLower() != "none")
                     {
                         user.USER_PASSWD = CentralismSecurity.DencryptPassword(user.USER_PASSWD);
                     }*/
                    Session["HotelsRole"] = hHotelsRoles;
                }
                return (IList<H_Hotels_Role>)Session["HotelsRole"];
            }
        }


    }
}
